package com.changgou.user.service.impl;


import com.changgou.user.config.TokenDecode;
import com.changgou.user.dao.AddressMapper;
import com.changgou.user.dao.UserMapper;
import com.changgou.user.pojo.Address;
import com.changgou.user.pojo.Info;
import com.changgou.user.pojo.User;
import com.changgou.user.service.AddressService;
import com.changgou.user.service.InfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Map;

/**
 * InfoServiceImpl  TODO
 * 13418  computer
 *
 * @author zhougang
 * @date 2020/4/26 15:52
 */
@Service
public class InfoServiceImpl implements InfoService {

    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private TokenDecode tokenDecode;

    @Override
    public void add(Info info) {
        Map<String, String> userInfo = tokenDecode.getUserInfo();
        String username = userInfo.get("username");

        String provinceid = info.getProvinceid();
        String cityid = info.getCityid();
        String areaid = info.getAreaid();
        String job = info.getJob();

        Address address = new Address();
        address.setJob(job);
        address.setProvinceid(provinceid);
        address.setCityid(cityid);
        address.setAreaid(areaid);
        address.setUsername(username);
        addressMapper.insertSelective(address);

        User user = new User();
        user.setUsername(username);
        user.setSex(info.getSex());
        user.setNickName(info.getNickName());
        Example example=new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username", username);
        userMapper.updateByExampleSelective(user, example);
    }
}
